package com.ax.business.prescription.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.ax.base.system.domain.SysUserDO;
import com.ax.business.common.dao.BaseDoctorMapper;
import com.ax.business.common.util.PageUtils;
import com.ax.business.common.util.Query;
import com.ax.business.prescription.dao.PrescriptionDetailMapper;
import com.ax.business.prescription.dao.PrescriptionMastMapper;
import com.ax.business.prescription.dao.BaseAssociatorMapper;
import com.ax.business.prescription.dao.PrescriptionAuditMapper;
import com.ax.business.prescription.domain.BaseAssociator;
import com.ax.business.prescription.domain.PrescriptionAudit;
import com.ax.business.prescription.domain.PrescriptionDetail;
import com.ax.business.prescription.domain.PrescriptionMast;
import com.ax.business.prescription.service.PrescriptionService;
import com.google.gson.Gson;
@Service("PrescriptionService")
public class PrescriptionServiceImpl implements PrescriptionService {
	@Autowired
	BaseDoctorMapper BaseAssociatorMapper;
	
	@Autowired
	PrescriptionDetailMapper PrescriptionDetailMapper;
	
	@Autowired
	PrescriptionMastMapper prescriptionMastMapper;
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@Autowired
	PrescriptionAuditMapper PrescriptionAuditMapper;
	
	@Autowired
	BaseAssociatorMapper baseAssociatorMapper;

	/**
	 * 查看个人处方详情(或全部)
	 * 
	 * @param Map<String, Object> map,request
	 *           
	 * @return PageUtils
	 */
	@Override
	public PageUtils queryPrescriptionSer(Map<String, Object> params,HttpServletRequest request){
		Object sessionId = request.getSession().getAttribute("sessionId");
		Gson gson = new Gson();
		SysUserDO user = null;
		String userJson = redisTemplate.opsForValue().get(sessionId);
		if(StringUtils.isNotEmpty(userJson)){
		user =  gson.fromJson(userJson, SysUserDO.class);
		}
		//分情况查询(2.医师1.药监(/系统))
		String typeType = user.getUserTypeCode();
		if(typeType.equals("2")){
		params.put("doctorId",user.getUserId());
		}
		Query query = new Query(params);
		List<PrescriptionMast> BaseDoctors = prescriptionMastMapper.queryPrescriptionMapper(query);
		Integer count = prescriptionMastMapper.count(query);
		PageUtils pageUtil = new PageUtils(BaseDoctors,count);
		return pageUtil;
	}
	
	/**
	 * 查看处方审核详情(或全部)
	 * 
	 * @param Map<String, Object> map,request
	 *           
	 * @return PageUtils
	 */
	@Override
	public PageUtils queryPrescriptionAuditingSer(Map<String, Object> params,HttpServletRequest request) {
		Object sessionId = request.getSession().getAttribute("sessionId");
		Gson gson = new Gson();
		SysUserDO user = null;
		String userJson = redisTemplate.opsForValue().get(sessionId);
		if(StringUtils.isNotEmpty(userJson)){
		user =  gson.fromJson(userJson, SysUserDO.class);
		}
		//分情况查询(3.药师1.药监(/系统))
		String typeType = user.getUserTypeCode();
		Query query = new Query(params);
		if(typeType.equals("3")){
		params.put("pharmacistId",user.getUserId());
		}
		List<PrescriptionAudit> PrescriptionAudits = PrescriptionAuditMapper.queryPrescriptionAuditingMapper(query);
		Integer count = PrescriptionAuditMapper.count(query);
		PageUtils pageUtil = new PageUtils(PrescriptionAudits,count);
		return pageUtil;
	}

	/**
	 * 新增处方(模块:(新增)会员(*会员头像的截取),处方审核信息,摄像头视频录制保存)
	 * 
	 * @param PrescriptionMast prescriptionMast,BaseAssociator baseAssociator,PrescriptionAudit prescriptionAudit
	 * String[] parameter
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertPrescriptionSer(HttpServletRequest request,PrescriptionMast prescriptionMast,BaseAssociator baseAssociator, PrescriptionAudit prescriptionAudit,String[] parameter) {
		//验证问诊人是否在系统中存在
		Integer ifexist = baseAssociatorMapper.IfexistAssociator(prescriptionMast.getPersonId());
		Object sessionId = request.getSession().getAttribute("sessionId");
		Gson gson = new Gson();
		SysUserDO user = null;
		String userJson = redisTemplate.opsForValue().get(sessionId);
		if(StringUtils.isNotEmpty(userJson)){
		user =  gson.fromJson(userJson, SysUserDO.class);
		}
		Long associatorId;
		if(ifexist == 0){
		baseAssociator.setDrugstoreId(prescriptionMast.getDrugstoreId());
		baseAssociator.setDrugstoreName(prescriptionMast.getDrugstoreName());
		baseAssociator.setRegistrarId(user.getUserId());
		baseAssociator.setRegistrarName(user.getName());
		baseAssociator.setFlag(1);
		baseAssociatorMapper.insertSelective(baseAssociator);
		associatorId = baseAssociator.getAssociatorId();
		}else{
		associatorId = prescriptionMast.getAssociatorId();
		}
		prescriptionMast.setDoctorId(user.getUserId());
		prescriptionMast.setDoctorName(user.getName());
		prescriptionMast.setDoctorPhone(user.getMobile());
		prescriptionMast.setAssociatorId(associatorId);
		prescriptionMastMapper.insertSelective(prescriptionMast);
		//处方明细(0药品ID///1药品名称///2规格///3数量///4是否含麻药///5用法用量///6备注)
		PrescriptionDetail PrescriptionDetail = new PrescriptionDetail();
		PrescriptionDetail.setPrescriptionId(prescriptionMast.getPrescriptionId());
		PrescriptionDetail.setPrescriptionSeq(prescriptionMast.getPrescriptionSeq());
		for(String pt : parameter){
		String[] details = pt.split("///");
		PrescriptionDetail.setDrugId(Long.parseLong(details[0]));
		PrescriptionDetail.setDrugName(details[1]);
		PrescriptionDetail.setSpec(details[1]);
		PrescriptionDetail.setNumber(Float.valueOf(details[1]));
		PrescriptionDetail.setIsHemp(Integer.parseInt(details[1]));
		PrescriptionDetail.setTakeMethod(details[1]);
		PrescriptionDetail.setRemarks(details[1]);
		PrescriptionDetailMapper.insertSelective(PrescriptionDetail);
		}
	}

	/**
	 * 处方审核(模块:药师审核处方(分两种1.本系统医师所开处方2.非本系统医师所开处方))
	 * 
	 * @param PrescriptionMast prescriptionMast,PrescriptionAudit prescriptionAudit,Integer,type(0.本系统1.非本系统) 
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertOrUpdateAuditingSer(Integer type,HttpServletRequest request,PrescriptionMast prescriptionMast,PrescriptionAudit prescriptionAudit) {
		Object sessionId = request.getSession().getAttribute("sessionId");
		Gson gson = new Gson();
		SysUserDO user = null;
		String userJson = redisTemplate.opsForValue().get(sessionId);
		if(StringUtils.isNotEmpty(userJson)){
		user =  gson.fromJson(userJson, SysUserDO.class);
		}
		Long associatorId;
		if(type == 1){
		//验证问诊人是否在系统中存在
		Integer ifexist = baseAssociatorMapper.IfexistAssociator(prescriptionMast.getPersonId());
		if(ifexist == 0){
		BaseAssociator baseAssociator = new BaseAssociator();
		baseAssociator.setDrugstoreId(prescriptionMast.getDrugstoreId());
		baseAssociator.setDrugstoreName(prescriptionMast.getDrugstoreName());
		baseAssociator.setRegistrarId(user.getUserId());
		baseAssociator.setRegistrarName(user.getName());
		baseAssociator.setFlag(1);
		baseAssociatorMapper.insertSelective(baseAssociator);
		associatorId = baseAssociator.getAssociatorId();
		}else{
		associatorId = prescriptionMast.getAssociatorId();
		  }
		prescriptionAudit.setAssociatorId(associatorId);
		prescriptionAudit.setPharmacistId(user.getUserId());
		prescriptionAudit.setPharmacistName(user.getName());
		PrescriptionAuditMapper.insertSelective(prescriptionAudit);
		}else{
		prescriptionMast.setPharmacistId(user.getUserId());
		prescriptionMast.setPharmacistName(user.getName());
		prescriptionMast.setPharmacistPhone(user.getMobile());
		prescriptionMastMapper.updateByPrimaryKeySelective(prescriptionMast);
		}
	}
}
